﻿Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS
''' <summary>
''' ARCGIS 图形类工具
''' 蒋宏虎20120212
''' </summary>
''' 
''' <remarks></remarks>
Public Class ArcgisGraphTool
    ''' <summary>
    ''' 使用Arcgis多边形是否包含来判断最小环
    ''' </summary>
    ''' <param name="tmpRing1"></param>
    ''' <param name="tmpRing2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function isInterSectRingArcGis(ByRef tmpRing1 As IClassTypeRing, ByRef tmpRing2 As IClassTypeRing) As Integer
        Dim ring1Polygon As Polygon = Nothing
        Dim ring2polygon As Polygon = Nothing
        Dim iop1 As IRelationalOperator = Nothing
        Dim iop2 As IRelationalOperator = Nothing
        Dim apoint As IPoint = Nothing
        'tmpRing1.GeometryObject = Nothing
        'tmpRing2.GeometryObject = Nothing
        Dim save1Polygon As Polygon = Nothing
        Dim save2polygon As Polygon = Nothing
        Try
            'If tmpRing1.GeometryObject Is Nothing Then
            'Else
            '    save1Polygon = tmpRing1.GeometryObject
            'End If

            'If tmpRing2.GeometryObject Is Nothing Then
            'Else
            '    save2polygon = tmpRing2.GeometryObject
            'End If

            tmpRing1.GeometryObject = Nothing
            tmpRing2.GeometryObject = Nothing

            If tmpRing1.GeometryObject Is Nothing Then
                ring1Polygon = New Polygon()
                For Each tmpNode As TypeNode In tmpRing1.getNodeList
                    apoint = New Point()
                    apoint.X = tmpNode.longitude
                    apoint.Y = tmpNode.latitude
                    ring1Polygon.AddPoint(apoint)
                    apoint.SetEmpty()
                    '  System.Runtime.InteropServices.Marshal.ReleaseComObject(apoint)
                    apoint = Nothing
                Next
                '形成闭合环
                apoint = New Point
                apoint.X = tmpRing1.getStartNode.longitude
                apoint.Y = tmpRing1.getStartNode.latitude
                ring1Polygon.AddPoint(apoint)
                apoint.SetEmpty()
                '  System.Runtime.InteropServices.Marshal.ReleaseComObject(apoint)
                apoint = Nothing

                tmpRing1.GeometryObject = ring1Polygon
            Else
                ring1Polygon = tmpRing1.GeometryObject
            End If

            '构建比较器
            iop1 = ring1Polygon
            If tmpRing2.GeometryObject Is Nothing Then

                ring2polygon = New Polygon()
                For Each tmpNode As TypeNode In tmpRing2.getNodeList
                    apoint = New Point
                    apoint.X = tmpNode.longitude
                    apoint.Y = tmpNode.latitude
                    ring2polygon.AddPoint(apoint)
                    apoint.SetEmpty()
                    '   System.Runtime.InteropServices.Marshal.ReleaseComObject(apoint)
                    apoint = Nothing
                Next
                '形成闭合环
                apoint = New Point
                apoint.X = tmpRing2.getStartNode.longitude
                apoint.Y = tmpRing2.getStartNode.latitude
                ring2polygon.AddPoint(apoint)

                apoint.SetEmpty()
                '   System.Runtime.InteropServices.Marshal.ReleaseComObject(apoint)
                apoint = Nothing
                tmpRing2.GeometryObject = ring2polygon
            Else
                ring2polygon = tmpRing2.GeometryObject
            End If

            iop2 = ring2polygon


            Dim ipolyclass1 As PolygonClass = ring1Polygon
            Dim ipolyclass2 As PolygonClass = ring2polygon


            ipolyclass1.SnapToSpatialReference()
            ipolyclass2.SnapToSpatialReference()

            If Math.Sign(ipolyclass1.Area) <> Math.Sign(ipolyclass2.Area) Then
                ipolyclass1.ReverseOrientation()
            End If

            If iop1.Disjoint(ring2polygon) Then
                isInterSectRingArcGis = 0
            Else
                'If iop1.Contains(ring2polygon) Then
                '    isInterSectRingArcGis = 1
                'End If



                If iop1.Contains(ring2polygon) Then
                    isInterSectRingArcGis = 1
                ElseIf iop1.Within(ring2polygon) Then
                    isInterSectRingArcGis = -1
                Else
                    isInterSectRingArcGis = 0
                End If
            End If
                'If Not save2polygon Is Nothing Then
                '    Dim ipoly1 As PolygonClass = save2polygon
                '    Dim aaa = iop1.Contains(ipoly1)
                '    If aaa = True Then
                '        If isInterSectRingArcGis = 1 Then
                '        Else
                '            Dim bbbb = 1
                '        End If
                '    End If
                '    Dim aaa1 = iop1.Within(ipoly1)
                '    If aaa1 = True Then
                '        If isInterSectRingArcGis = -1 Then
                '        Else
                '            Dim bbbb = 1
                '        End If
                '    End If
                'End If

                'ring2polygon.RemovePoints(0, ring2polygon.PointCount)
                'ring1Polygon.RemovePoints(0, ring1Polygon.PointCount)
        Catch e As Exception
            Debug.Print(e.Message)
            MsgBox(e.Message, vbOKOnly)

        Finally
            Try
                ring2polygon = Nothing
                ring1Polygon = Nothing
                iop2 = Nothing
                iop1 = Nothing
            Catch ex As Exception

            End Try

        End Try



    End Function



    Private Shared m_AOLicenseInitializer As LicenseInitializer
 

    Public Sub New()
        If ArcgisCurrentVersion = ArcGisVersion.ARCGIS_10 Then
            m_AOLicenseInitializer = New LicenseInitializer()
            m_AOLicenseInitializer.InitializeApplication(New esriLicenseProductCode() {esriLicenseProductCode.esriLicenseProductCodeArcEditor}, New esriLicenseExtensionCode() {})
        End If
    End Sub

    Public Sub Destruct()
        If ArcgisCurrentVersion = ArcGisVersion.ARCGIS_10 Then
            m_AOLicenseInitializer.ShutdownApplication()
        End If
    End Sub

    Property ArcgisCurrentVersion As ArcGisVersion = ArcGisVersion.ARCGIS_10
    ''' <summary>
    ''' ARCGIS 版本
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum ArcGisVersion As Integer
        ARCGIS_9 = 9   'ARCGIS 9.0以前版本
        ARCGIS_10 = 10 'arcgis 10.0 版本
    End Enum
End Class
